﻿using Prism.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrismDialog
{
    public class MainViewModel
    {
        private readonly IEventAggregator eventAggregator;
        private readonly IDialogService dialogService;

        public DelegateCommand OpenCommand { get; set; }

        // 属性注入：IOC  MainViewModel必须由IOC容器创建
        //[Dependency]
        //IEventAggregator eventAggregator;

        public MainViewModel(IEventAggregator eventAggregator,IDialogService dialogService)
        {
            OpenCommand = new DelegateCommand(OnOpen);
            this.eventAggregator = eventAggregator;
            this.dialogService = dialogService;
        }

        private void OnOpen()
        {
            // 使用消息处理
            //eventAggregator.GetEvent<EventMessage>().Publish("123");

            // 不使用消息处理
            // 使用DialogService
            //dialogService.ShowDialog("UCDetail");
            //IDialogServiceExtensions.ShowDialog(dialogService, "UCDetail");

            //dialogService.ShowDialog("UCDetail",new Action<IDialogResult>(OnDialogClosed));

            DialogParameters param = new DialogParameters();
            param.Add("B", 222);
            dialogService.ShowDialog("UCDetail", param, new Action<IDialogResult>(OnDialogClosed));
        }
        private void OnDialogClosed(IDialogResult result) 
        {
            string aa = result.Parameters.GetValue<string>("A");
        }
    }
}
